package com.woniuxy.controller.goodsGeneralAndSmallAndBrand;


import com.woniuxy.demo.Result;
import com.woniuxy.dto.goodsGeneralAndSmallAndBrand.GoodsBrandDto;
import com.woniuxy.dto.goodsGeneralAndSmallAndBrand.UpdateGoodsBrandDto;
import com.woniuxy.entity.GoodsBrand;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.AddGoodsBrandParam;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.DeleteBrandParam;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.ListGoodsBrandParam;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.UpdateGoodsBrandParam;
import com.woniuxy.service.GoodsBrandService;
import com.woniuxy.util.ValidatedUtil;
import com.woniuxy.util.WoniuBeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zcx,ftl,gr,rhs
 * @since 2021-12-31
 * 关于品牌信息管理的接口
 */
@RestController
@Api(tags = "关于品牌信息管理的接口")
@RequestMapping("/goods-brand")
public class GoodsBrandController {

    @Autowired
    private GoodsBrandService goodsBrandService;


    /**
     * 新增大类信息后端接口
     *
     * @param bindingResult 参数验证
     * @param addGoodsBrandParam 前端传递的新增品牌信息数据
     * @return 返回给前端的新增品牌信息数据
     */
    @PostMapping("/addBrandInfo")
    @ApiOperation("新增品牌信息")
    public Result addBrandInfo(@Validated @RequestBody  AddGoodsBrandParam addGoodsBrandParam,BindingResult bindingResult) {
        //参数验证
        ValidatedUtil.ValidatedException(bindingResult);
        return Result.newInstance().setCode(200).setMsg("新增成功")
                .setData(goodsBrandService.saveBrandInfo(addGoodsBrandParam));
    }

    /**
     * 分页查询品牌信息后端接口
     *
     * @param bindingResult 参数验证
     * @param listGoodsBrandParam 前端传递的分页查询品牌信息的条件和模糊查询的条件
     * @return 分页查询得到的品牌信息的数据集合
     */
    @PostMapping("/pageBrandInfo")
    @ApiOperation("分页查询品牌信息")
    public Result pageBrandInfo(@Validated @RequestBody ListGoodsBrandParam listGoodsBrandParam,BindingResult bindingResult) {
        //参数验证
        ValidatedUtil.ValidatedException(bindingResult);
        return Result.newInstance().setCode(200).setMsg("查询成功")
                .setData(goodsBrandService.pageFind(listGoodsBrandParam));
    }

    /**
     * 修改品牌信息后端接口
     *
     * @param bindingResult 参数验证
     * @param updateGoodsBrandParam 前段传递的修改的品牌信息数据
     * @return 返回给前端的修改后的品牌信息数据
     */
    @PostMapping("/updateBrandInfo")
    @ApiOperation("修改品牌信息")
    public Result updateBrandInfo(@Validated @RequestBody  UpdateGoodsBrandParam updateGoodsBrandParam,BindingResult bindingResult) {
        //参数验证
        ValidatedUtil.ValidatedException(bindingResult);
        Boolean b = goodsBrandService.updateBrandInfo(updateGoodsBrandParam);
        if (b){
            return Result.newInstance().setCode(200).setMsg("修改成功");
        }
            return Result.newInstance().setCode(500).setMsg("修改失败");

    }


    /**
     * 批量删除品牌信息后端接口
     *
     * @param bindingResult 参数验证
     * @param goodsBrandIds 前端传递的批量删除的品牌信息id集合
     * @return
     */
    @PostMapping("/removeBrandInfo")
    @ApiOperation("批量删除品牌信息")
    public Result removeBrandInfo(@Validated   @RequestBody List<Integer> goodsBrandIds, BindingResult bindingResult) {
        //参数验证
        ValidatedUtil.ValidatedException(bindingResult);
        if (goodsBrandService.deleteGoodsBrand(goodsBrandIds))
            return Result.newInstance().setCode(200).setMsg("删除成功");
        return Result.newInstance().setCode(500).setMsg("删除失败");
    }


    /**
     * 根据品牌id查询品牌信息
     * @param id 品牌信息的id
     * @return  根据品牌id查询到的品牌信息数据
     */
    @GetMapping("/getBrandById/{id}")
    @ApiOperation("单个查询大类信息")
    public Result getBrandById(@PathVariable("id") Integer id) {
        GoodsBrandDto goodsBrandDto = goodsBrandService.getBrandById(id);
        return Result.newInstance().setCode(200).setMsg("查询成功")
                .setData(goodsBrandDto);
    }

    @ApiOperation("查询所有品牌信息")
    @GetMapping("/listAllBrand")
    public Result listAllBrand(){
        List<GoodsBrand> brandList = goodsBrandService.list();
        List<GoodsBrandDto> goodsBrandDtos = WoniuBeanUtils.copyList(brandList, GoodsBrandDto.class);
        return Result.newInstance().setCode(200).setMsg("查询成功").setData(goodsBrandDtos);
    }
}

